<?php 
namespace app\models;
use Yii;
use yii\db\ActiveRecord;

class Options extends ActiveRecord
{
    public static function tableName()
    {
        return 'options';
    }
    public function addOptions($post,$num)
    {
        // 按顺序给options update
        /*
	1、blogname
	2、siteurl
        */
        // $tmp 用于判断case 'nav_card':内的代码有无执行，既有无提交nav_card 
        $temp =0;
        foreach ($post as $key => $value) {
            # code...
            Yii::trace($key);
            switch ($key) {
                case 'blogname':
                    $option = Options::find()->where(['option_name'=>'blogname'])->one();
                    $option->option_value = $value;
                    $option->save();
                    break;
                 case 'siteurl':
                    $option = Options::find()->where(['option_name'=>'siteurl'])->one();
                    $option->option_value = $value;
                    $option->save();
                    break;
                  case 'introduction':
                    $option = Options::find()->where(['option_name'=>'introduction'])->one();
                    $option->option_value = $value;
                    $option->save();
                    break;
                case 'nav_card':
                    // 当post中无nav_card属性时不执行以下代码
                    $option = Options::find()->where(['option_name'=>'nav_card'])->one();
                    $option->option_value = $value;
                    $option->save();
                    $temp = 1;
                    break;
            }

        }
        // 当nav_card 无提交时，更改其值为none
        if($temp==0)
        {
             $option = Options::find()->where(['option_name'=>'nav_card'])->one();
              $option->option_value = 'none';
              $option->save();
        }
        // for($i=1;$i<$num+1;$i++)
        // {
              // Yii::trace($post);
                //  if($i=3&&)                     
                // {
                //     $option = Options::findOne($i);
                //     $option->option_value= "none";
                //     $option->save(); 
                //     continue;
                // }
                // $option = Options::findOne($i);
                // $option->option_value = $post[$i];
                // $option->save(); 
        
        // 独自为属性hide_navCard update

    }
    public static function getOptions()
    {
    	// $os['blogname']="";
    	// $os['siteurl']="";
    	$option = Options::find()->all();
    	foreach ($option as $key => $val) {
    		switch ($val->option_name) {
    			case 'blogname':
    				$os['blogname'] = $val->option_value;
    				break;
    			case 'siteurl':
    				$os['siteurl'] = $val->option_value;
    				break;
                                                case 'nav_card':
                                                              $os['nav_card'] = $val->option_value=='display'?'1':'0';
                                                              break;
                                                case 'introduction':
                                                              $os['introduction'] = $val->option_value;
                                                              break;
    			
    			
    		}
    	}
    	return $os;
    }
    //通过属性名来获得属性值
    public static function getOption($option)
    {
         $ob = Options::find()->where(['option_name'=>$option])->one();
         return $ob->option_value;
    }
}


 ?>